﻿using System.Data;
using System.Web;
using Me.Amon.C;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.POS;
using Me.Amon.M;
using Me.Amon.POS.C;
using Me.Amon.POS.M;
using Me.Amon.Util;
using Newtonsoft.Json;

namespace Me.Amon.POS
{
    /// <summary>
    /// Emp 的摘要说明
    /// </summary>
    public class Emp : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            var obj = new MEmp();

            // 公司
            var corp = context.Request[PPos.PARAM_KEY_CORP];
            if (string.IsNullOrEmpty(corp))
            {
                obj.error_code = PPos.ERROR_CODE_CORP_EMPTY;
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }

            // 操作
            var opt = context.Request[CObj.PARAM_KEY_OPT];
            if (string.IsNullOrEmpty(opt))
            {
                obj.error_code = PObj.ERROR_CODE_OPT_EMPTY;
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }

            // 数据
            var dat = context.Request[PPos.PARAM_KEY_DATA];
            if (string.IsNullOrWhiteSpace(dat))
            {
                obj.error_code = PPos.ERROR_CODE_DAT_EMPTY;
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }

            switch (opt)
            {
                case PPos.PARAM_VALUE_OPT_SELECT:
                    SelectEmp(corp, dat, context);
                    break;
                case PPos.PARAM_VALUE_OPT_CREATE:
                    break;
                case PPos.PARAM_VALUE_OPT_UPDATE:
                    break;
                case PPos.PARAM_VALUE_OPT_DELETE:
                    break;
                case "l":
                    LoginEmp(corp, dat, context);
                    break;
                default:
                    obj.error_code = PObj.ERROR_CODE_OPT_ERROR;
                    context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                    break;
            }
            context.Response.End();
        }

        private void SelectEmp(string corp, string key, HttpContext context)
        {
            var sPage = context.Request[PPos.PARAM_KEY_PAGE] ?? "";
            var iPage = 0;
            if (CharUtil.IsValidateLong(sPage))
            {
                iPage = int.Parse(sPage);
            }
            var sSize = context.Request[PPos.PARAM_KEY_PAGE_SIZE] ?? "";
            var iSize = 5;
            if (CharUtil.IsValidateLong(sSize))
            {
                iSize = int.Parse(sSize);
            }

            var dba = new RDBEngine();
            dba.AddTable(DEmp.TABLE);
            dba.AddWhere(DEmp.CORP_ID, corp);
            dba.AddWhere(DEmp.CHANNEL, CEmp.CHANNEL);
            dba.AddWhere(DEmp.STATUS, CEmp.STATUE_ENABLED.ToString());
            dba.AddWhere(DEmp.USER, key);

            var dt = dba.ExecuteSelect();
            var s = iPage * iSize;
            var t = s + iSize;
            var c = dt.Rows.Count;
            if (t > c)
            {
                t = c;
            }
            var emps = new MEmp[t - s];
            c = 0;
            while (s < t)
            {
                emps[c++] = ReadEmp(dt.Rows[s++]);
            }

            context.Response.Write(JsonConvert.SerializeObject(emps));
        }

        private MEmp ReadEmp(DataRow row)
        {
            var emp = new MEmp();
            emp.code = row[DEmp.CODE] as string;
            emp.actor = (int)row[DEmp.ACTOR];
            emp.user = row[DEmp.USER] as string;
            emp.first_name = row[DEmp.FIRST_NAME] as string;
            emp.last_name = row[DEmp.LAST_NAME] as string;
            emp.display_name = row[DEmp.DISPLAY_NAME] as string;
            emp.cellphone = row[DEmp.CELLPHONE] as string;
            emp.telephone = row[DEmp.TELEPHONE] as string;
            return emp;
        }

        private void LoginEmp(string corp, string pass, HttpContext context)
        {
            // 用户名
            var user = context.Request[PPos.PARAM_KEY_USER];
            if (string.IsNullOrWhiteSpace(user))
            {
                var obj = new MObj();
                obj.error_code = PPos.ERROR_CODE_USER_EMPTY;
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }

            //收银机
            var code = context.Request["t"];
            if (string.IsNullOrWhiteSpace(code))
            {
                var obj = new MObj();
                obj.error_code = PObj.ERROR_CODE_CUSTOM;
                obj.error_message = "终端代码为空！";
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }

            var dba = new RDBEngine();
            dba.AddTable(DEmp.TABLE);
            dba.AddWhere(DEmp.CORP_ID, corp);
            dba.AddWhere(DEmp.USER, user);
            dba.AddWhere(DEmp.PASS, pass);

            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count < 1)
            {
                var obj = new MObj();
                obj.error_code = PObj.ERROR_CODE_CUSTOM;
                obj.error_message = "用户名或密码错误！";
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }

            var emp = ReadEmp(dt.Rows[0]);
            context.Response.Write(JsonConvert.SerializeObject(emp));

            // 待以后处理
            //dba.Reset();
            //dba.AddTable(DPosTerminal.TABLE);
            //dba.AddWhere(DPosTerminal.CODE, code);

            //var dt = dba.ExecuteSelect();
            //if (dt.Rows.Count < 1)
            //{
            //    obj.error_code = CObj.ERROR_CODE_CUSTOM;
            //    obj.error_message = "用户名或密码错误！";
            //    context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
            //    context.Response.End();
            //    return;
            //}
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}